﻿using System;
using System.Windows.Forms;

namespace Amgine
{
    partial class NoteEditForm : Form
    {
        private Note note;
        private bool canEdit;

        public NoteEditForm(Note note, bool canEdit)
        {
            InitializeComponent();
            this.note = (note == null ? new Note() : note);
            this.canEdit = canEdit;
        }

        private void NoteEditForm_Load(object sender, EventArgs e)
        {
            textBox_title.Text = note.Title;
            textBox_info.Text = note.Info;

            if (!canEdit)
            {
                Text = "详细信息";
                textBox_title.ReadOnly = true;
                textBox_info.ReadOnly = true;
            }
        }

        private void button_confirm_Click(object sender, EventArgs e)
        {
            if (canEdit)
            {
                if (string.IsNullOrWhiteSpace(textBox_title.Text))
                {
                    MessageBox.Show("标题不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (string.IsNullOrWhiteSpace(textBox_info.Text))
                {
                    MessageBox.Show("内容不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    note.Title = textBox_title.Text;
                    note.Info = textBox_info.Text;
                    if (note.createOrUpdate())
                    {
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        DialogResult = DialogResult.Retry;
                    }
                    Close();
                }
            }
            else
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            if (canEdit)
            {
                textBox_title.Text = "";
                textBox_info.Text = "";
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
